package com.lanf.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanf.business.model.Tbpatients;
import com.lanf.business.vo.TbpatientsQueryVo;
import com.lanf.business.vo.TbpatientsExportVo;
import com.lanf.business.mapper.TbpatientsMapper;
import com.lanf.business.service.TbpatientsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.ArrayList;
import org.springframework.beans.BeanUtils;
import com.lanf.common.result.ResultCodeEnum;
import com.lanf.system.exception.LanfException;
import com.lanf.system.easyexcel.ExcelHandler;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
/**
* @author tanlingfei
* @version 1.0
* @description 患者信息表 Service实现类
* @date 2024-04-19 21:35:47
*/
@Transactional
@Service
public class TbpatientsServiceImpl extends ServiceImpl
<TbpatientsMapper, Tbpatients> implements TbpatientsService {
    @Autowired
    private TbpatientsMapper tbpatientsMapper;
    @Resource
    private ExcelHandler excelHandler;

    @Override
    public IPage<Tbpatients> selectPage(Page<Tbpatients> pageParam, TbpatientsQueryVo tbpatientsQueryVo) {
        //QueryWrapper<Tbpatients> queryWrapper = new QueryWrapper<>();
        //return tbpatientsMapper.selectPage(pageParam,queryWrapper);
        return tbpatientsMapper.selectPage(pageParam,tbpatientsQueryVo);
    }

    @Override
    public List<Tbpatients> queryList(TbpatientsQueryVo tbpatientsQueryVo){
     List<Tbpatients> result = tbpatientsMapper.queryList(tbpatientsQueryVo);
     return result;
   }

    @Override
    public boolean save(Tbpatients tbpatients){
        int result = this.tbpatientsMapper.insert(tbpatients);
        return result>0;
    }
    @Override
    public boolean updateById(Tbpatients tbpatients){
        int row = this.tbpatientsMapper.updateById(tbpatients);
        if(row <= 0){
            throw new LanfException(ResultCodeEnum.UPDATE_ERROR);
         }
         return row>0;
    }
    @Override
    public Tbpatients getById(String id){
         Tbpatients tbpatients = tbpatientsMapper.selectById(id);
         return tbpatients;
    }
   @Override
   public List<Tbpatients> getByIds(List<String> ids) {
      List<Tbpatients> list = this.tbpatientsMapper.selectBatchIds(ids);
      return list;
   }
   @Override
   public void exportData(HttpServletResponse response) {
   List<Tbpatients> list = this.selectPage(new Page<Tbpatients>(1, 10000), new TbpatientsQueryVo()).getRecords();
   List<TbpatientsExportVo> expList = new ArrayList<>();
   list.forEach(po -> {
       TbpatientsExportVo vo = new TbpatientsExportVo();
       BeanUtils.copyProperties(po, vo);
          expList.add(vo);
      });
      try {
          this.excelHandler.exportExcel(response, expList, TbpatientsExportVo.class, "患者信息表数据", "患者信息表数据");
      } catch (Exception e) {
          e.printStackTrace();
          throw new LanfException(9005, "导出失败");
      }
   }
}
